package wjl.java.base.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebUtil {
    public static void main( String[] args ) {

    }

    /**
     * 从网络下载文件
     * 
     * @param Url
     * @return
     */
    public static byte[] downloadFile( String Url ) {
        byte[]            data = null;
        InputStream       is   = null;
        HttpURLConnection conn = null;
        try{
            URL url = new URL( Url );
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput( true );
            conn.setRequestMethod( "GET" );
            conn.setConnectTimeout( 6000 );
            is = conn.getInputStream();
            if ( conn.getResponseCode() == 200 ){
                System.out.println( "正在下载文件......" );
                data = FileUtil.readInputStream( is );
                System.out.println( "下载完成......" );
            } else{
                data = null;
            }
        } catch (IOException e){
            e.printStackTrace();
        } finally{
            try{
                if ( is != null ){
                    is.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
            conn.disconnect();
        }
        return data;
    }
}
